﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>角色管理</title>
    <link rel="stylesheet" href="../../../assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../../assets/module/admin.css?v=317"/>
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">课程名称:</label>
                        <div class="layui-input-inline">
                            <input name="trainName" class="layui-input" placeholder="输入角色名"/>
                        </div>
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="roleTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="roleTable" lay-filter="roleTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="roleTbBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">查看</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="student">签到学员</a>
</script>
<script type="text/html" id="studentList">
    <table id="studentTable"></table>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="roleEditDialog">
    <form id="roleEditForm" lay-filter="roleEditForm" class="layui-form model-form">
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">课程名称:</label>
            <div class="layui-input-block">
                <input name="trainName" placeholder="请输入课程名称" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">培训讲师:</label>
            <div class="layui-input-block">
                <input name="userName" placeholder="请输入讲师名称" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注:</label>
            <div class="layui-input-block">
                <textarea name="comment" placeholder="备注" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">二维码:</label>
            <div class="layui-input-block">
                <a class="layui-btn" id="makeCode">点击获取二维码</a>
                <div id="qrcode" style="width:100px; height:100px; margin-top:15px;"></div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-submit" lay-filter="roleEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary layui-submit" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<!-- js部分 -->
<script type="text/javascript" src="../../../assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="../../../assets/js/common.js?v=317"></script>
<script type="text/javascript" src="../../../assets/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="../../../assets/js/js.cookie.min.js"></script>
<script type="text/javascript" src="../../../assets/js/sqjx.js"></script>
<script src="js/qrcode.js"></script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'zTree'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#roleTable',
            url: '/office/hrTrainClass/queryHrTrainClassList',
            page: true,
            method: 'post',
            request: {
                pageName: 'page',
                limitName: 'rows',
            },
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '</p>'].join(''),
            cellMinWidth: 100,
            height: $(window).height() - 100,
            cols: [[
                {type: 'numbers'},
                {field: 'trainName', title: '培训课程', sort: false},
                {field: 'userName', title: '培训讲师', sort: false},
                {field: 'comment', title: '备注', sort: false},
                {title: '操作', toolbar: '#roleTbBar', align: 'center', width: 200, minWidth: 200}
            ]],
            parseData: function (res) {
                return {
                    "code": 0,
                    "msg": "",
                    "count": res.obj.total,
                    "data": res.obj.rows
                }
            }
        });

        /* 表格搜索 */
        form.on('submit(roleTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(roleTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                showEditModel(obj.data);
            } else if (obj.event === 'auth') {  // 权限管理
                showPermModel(obj.data.roleId);
            } else if (obj.event === 'student') {
                showStudentModal(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(roleTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                showEditModel(obj);
            }
        });

        function showStudentModal(obj) {
           admin.open({
               type: 1,
               title: '学员列表',
               content: $('#studentList').html(),
               area: ['900px', '500px'],
           })
            table.render({
                elem: '#studentTable',
                url: '/office/hrEmployTrain/queryHrEmployTrainList?className='+obj.data.trainName,
                page: true,
                method: 'post',
                request: {
                    pageName: 'page',
                    limitName: 'rows',
                },
                cellMinWidth: 100,
                width:800,
                height: $(window).height() - 100,
                cols: [[
                    {type: 'numbers'},
                    {field: 'className', title: '培训课程', sort: false},
                    {field: 'userName', title: '培训讲师', sort: false},
                    {field: 'level', title: '评分', sort: false},
                    {field: 'comment', title: '备注', sort: false},
                ]],
                parseData: function (res) {
                    return {
                        "code": 0,
                        "msg": "",
                        "count": res.obj.total,
                        "data": res.obj.rows
                    }
                }

        })
        }

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? '查看' : '添加') + '课程',
                content: $('#roleEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    var qrcode = new QRCode(document.getElementById("qrcode"), {
                        width: 100,
                        height: 100
                    });
                    $("#makeCode").click(function () {
                        makeCode()
                    });
                    if (mData) {
                        $(".layui-submit").hide()
                    } else {
                        $(".layui-submit").show()
                    }

                    function makeCode() {
                        let baseUrl="http://47.104.72.230/#/pages/index/sign";//h5地址
                        var trainName = mData.trainName;
                        var userName = mData.userName;
                        let qrcodeUrl = baseUrl+'?trainName=' + trainName + '&userName=' + userName;
                        console.log(qrcodeUrl)
                        if (mData) {
                            qrcode.makeCode(qrcodeUrl);
                        } else {
                            if (trainName == '' || userName == '') {
                                showAlert('请补全表单');
                                return false;
                            }
                            qrcode.makeCode(qrcodeUrl);
                        }
                    }

                    form.val('roleEditForm', mData);
                    // 表单提交事件
                    form.on('submit(roleEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);
                        $.post('/office/hrTrainClass/addHrTrainClass', data.field, function (res) {
                            layer.close(loadIndex);
                            if (200 === res.code) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                }
            });
        }

        /* 权限管理 */
        function showPermModel(roleId) {
            admin.open({
                title: '角色权限分配',
                btn: ['保存', '取消'],
                content: '<ul id="roleAuthTree" class="ztree"></ul>',
                success: function (layero, dIndex) {
                    var loadIndex = layer.load(2);
                    $.post('/office/menu/queryCheckedMenu', {roleId: roleId}, function (res) {
                        res.obj.forEach(item => {
                            item.id = item.menuId;
                            item.open = true;
                            // item.checked = item.open == 1 ? true : false;
                            item.pId = item.pid;
                            item.name = item.menuName;
                        });
                        res.msg = res.msg;
                        res.data = res.obj;
                        layer.close(loadIndex);
                        if (200 === res.code) {
                            $.fn.zTree.init($('#roleAuthTree'), {
                                check: {enable: true},
                                data: {simpleData: {enable: true}}
                            }, res.data);
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'json');
                    // 超出一定高度滚动
                    $(layero).children('.layui-layer-content').css({'max-height': '300px', 'overflow': 'auto'});
                },
                yes: function (dIndex) {
                    var insTree = $.fn.zTree.getZTreeObj('roleAuthTree');
                    var checkedRows = insTree.getCheckedNodes(true);
                    var ids = [];
                    for (var i = 0; i < checkedRows.length; i++) {
                        ids.push(checkedRows[i].id);
                    }
                    var loadIndex = layer.load(2);
                    if (ids.length == 0) {
                        showAlert("请为角色配置菜单");
                        return false;
                    }
                    $.post('/office/role/setMenu', {roleId: roleId, menus: ids.join(',')}, function (res) {
                        layer.close(loadIndex);
                        if (200 === res.code) {
                            layer.msg(res.msg, {icon: 1});
                            layer.close(dIndex);
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'json');
                }
            });
        }

    });
</script>
</body>
</html>